package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.feign.ProductFeignService;
import com.atguigu.gulimall.ware.service.WareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Autowired
    private ProductFeignService productFeignService;

    @Autowired
    private WareInfoService wareInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        //用户输入的条件
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and( w ->{
               w.eq("purchase_id",key).or().eq("sku_id",key);
            });
        }
        //状态条件
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        //仓库id条件
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id",wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );
        List<PurchaseDetailEntity> records = page.getRecords();
        records = records.stream().map( item ->{
            WareInfoEntity wareInfoEntity = wareInfoService.getById(item.getWareId());
            item.setWareName(wareInfoEntity.getName());
            return item;
        }).collect(Collectors.toList());
        return new PageUtils(page);
    }

    @Override
    public void savePurchaseDetail(PurchaseDetailEntity purchaseDetail) {
        //获取当前商品的价格,要调用product服务
        BigDecimal price = getPurchasePrice(purchaseDetail.getSkuId());
        BigDecimal num = new BigDecimal(purchaseDetail.getSkuNum());
        purchaseDetail.setSkuPrice(price.multiply(num));
        this.save(purchaseDetail);

    }

    @Override
    public void updateDetailById(PurchaseDetailEntity purchaseDetail) {
        //获取当前商品的价格,要调用product服务
        BigDecimal price = getPurchasePrice(purchaseDetail.getSkuId());
        BigDecimal num = new BigDecimal(purchaseDetail.getSkuNum());
        purchaseDetail.setSkuPrice(price.multiply(num));
        //进行修改
        this.updateById(purchaseDetail);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_id",id);
        return this.list(wrapper);
    }

    public BigDecimal getPurchasePrice(Long id) {
        R info = productFeignService.getSkuBySkuId(id);
        Map<String,Object> data = (Map<String,Object>)info.get("skuInfo");
        if (info.getCode() == 0){
            String price = data.get("price").toString();
            return new BigDecimal(price);
        }
        return null;
    }

}